<?php
/*
 * @分销商城
 * @功能说明：客户管理中心
 * @更新说明：暂无更新
 * @文件名 IndexController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 下午1:46:53
 * @创建人 XING
 */
namespace cash\Controller;
use Think\Controller;
header("Content-type: text/html; charset=utf-8");
class IndexController extends ApibaseController {
	/*
	 * 用户提现审核
	 * @date: 2016-05-05 12:12:00
	 * @editor: WENCUN
	 */
    public function index(){
		if(session('loginType') != 1){
			redirect('/cash/index');exit;
		}
    	/*参数初始化*/
		$page = I('get.page')?:1;
		$perpage = 10;
		$url = array();
		$list = M('CustCash');
		if(I('get.cust_realname')){
			$list = $list->where("cust_realname LIKE '%".I('get.cust_realname')."%'");
			$url['cust_realname'] = I('get.cust_realname');
		}
		if(I('get.cust_mobile')){
			$list = $list->where(array('cust_mobile'=>I('get.cust_mobile')));
			$url['cust_mobile'] = I('get.cust_mobile');
		}
		if(I('get.check')!='all' && I('get.check') != ''){
			$list = $list->where(array('is_check'=>I('get.check')));
			$url['check'] = I('get.check');
		}
		$object = clone $list;
		$getData = $list->order(array('cust_cashid'=>'DESC'))->page($page,$perpage)->select();
		$this->assign('getData',$getData);
		$this->assign('page',setPage($object->count(),$perpage,$page,'?'.http_build_query($url)));
    	$this->display('index/index');
    }

	/*
	 * 分销商城_用户收入明细
	 * @date: 2016-06-01 20:01:00
	 * @editor: WENCUN
	 * */
	public function userMsg(){
		if(I('get.cust_id')){
			$page = I('get.page')?:1;
			$perpage = 10;
			$url = array();
			$list = M('ProfitEntity')->where(array('profit_cust_id'=>I('get.cust_id')));
			if(I('get.order_id')){
				$list = $list->where(array('order_id'=>I('get.order_id')));
				$url['order_id'] = I('get.order_id');
			}
			$object = clone $list;
			$getProfitMsg = $list->order(array('add_at'=>'DESC'))->page($page,$perpage)->select();
			$this->assign('page',setPage($object->count(),$perpage,$page,'?'.http_build_query($url)));
			$this->assign('getData',$getProfitMsg);
			$this->display('/index/usermsg');
		}
	}

	/*
	 * 分销商城_调取用户资金信息
	 * @date: 2016-06-01 20:20:00
	 * @editor: WENCUN
	 * */
	public function smoney(){
		if(I('get.type') && in_array(I('get.type'),array('1','2','3','4')) && I('get.cust_id')){
			switch(I('get.type')){
				/*待结算金额*/
				case '1':
					$getProfitNums = M('GoodsProfit')
						->where(array('biz_cust_id'=>I('get.cust_id')))->where('(is_profit = 0 OR is_profit = 3) AND pay_at > 0')->sum('profit * goods_nums');
					//总监待结算金额
					$zj = M('CustEntity')->where(array('cust_id'=>I('get.cust_id'),'salelevel'=>'3'))->count();
					if($zj > 0){
						$zjProfit = M('GoodsProfit')->where(array('zj_cust_id'=>I('get.cust_id')))->where('(is_profit = 0 OR is_profit = 3) AND pay_at > 0')->sum('zj_profit * goods_nums');
						if($zjProfit){
							$getProfitNums += $zjProfit;
						}
					}
					break;
				/*可提现金额*/
				case '2':
					$getProfitNums = M('CustBottler')
						->where(array('cust_id'=>I('get.cust_id'),'is_waste'=>'0'))->sum('finance_money');
					break;
				/*提现中金额*/
				case '3':
					$getProfitNums = M('CustCash')
						->where(array('cust_id'=>I('get.cust_id'),'cash_status'=>'0'))->where('is_check != -1')
						->sum('cash_money');
					break;
				/*已提现金额*/
				case '4':
					$getProfitNums = M('CustCash')
						->where(array('cust_id'=>I('get.cust_id'),'cash_status'=>'1','is_check'=>'1'))->sum('cash_money');
					break;
			}
			echo json_encode(array('code'=>'1','msg'=>'success','data'=>$getProfitNums ?: '0.00'));exit;
		}else{
			jsonRespons(0, '非法请求');
		}
	}
}